<!DOCTYPE html>
<html lang="zh-cn" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head th:replace="commons::common_header"><!-- 引入头文件 -->
</head>
<body>
<link rel="stylesheet" th:href="@{/static/css/zTreeStyle.css}"/>
<link rel="stylesheet" th:href="@{/static/css/jquery-ui.min.css}"/>
<link rel="stylesheet" th:href="@{/static/css/jquery-ui-1.10.4.custom.css}"/>
<link rel="stylesheet" th:href="@{/static/css/bootstrap-table.css}"/>
<link rel="stylesheet" th:href="@{/static/css/bootstrap-select.min.css}"/>
<nav th:replace="commons::common_nav"></nav><!-- 引入导航栏 -->
<div th:replace="commons::common_pwdmodal"></div><!-- 引入密码修改框 -->
<!-- 设备管理模块整理视图 -->
<div class="container" style="width: 1500px; margin-top: 50px">
    <div class="panel panel-default" style=" width: 98%; margin-left: 1%;">
        <!-- 表头功能按键 -->
        <div class="panel-heading">
            <div class="pull-right">
                <div class="btn-group">
                    <button type="button" class="btn btn-primary" onclick="userEdit(0)" id="userEdit">
                        新增用户
                    </button>
                </div>&nbsp;
                <div class="btn-group">
                    <button type="button" class="btn btn-success" onclick="location.reload()">
                        刷新
                    </button>
                </div>
            </div>
            <h5> 用户管理 </h5>
        </div>

        <!-- 表格主体 -->
        <form class="form-inline" role="form" id="formaboutuser" name="formaboutuser" action="/mlp/user/list"
              method="post">
            <div class="panel-body">
                <!-- 主体左侧树状目录 -->

                <!-- 主体右侧软件信息表格 -->
                <div class="panel panel-default col-sm-10" style="left:10px; width:1400px; height:650px;">
                    <div class="panel-heading " style="padding: 0px;">
                        <div style="display: inline-block;width: 100%; padding-top: 10px">
                            <div class=" col-sm-12">
                                <div class="btn-group"><!--suppress ALL-->
                                    <input size="20" type="text" id="username" name="username" th:value="${username}"
                                           placeholder="请输入用户名" maxlength="50"
                                           style="width: 150px;padding-bottom: 0px;padding-top: 0px;height: 34px;">
                                </div>
                                <div class="btn-group"><!--suppress ALL-->
                                    <input size="20" type="tel" id="phone" name="phone" th:value="${phone}"
                                           placeholder="请输入手机号" maxlength="11"
                                           style="width: 150px;padding-bottom: 0px;padding-top: 0px;height: 34px;"
                                           onkeyup="this.value=this.value.replace(/[^\uFF00-\uFFFF\u4e00-\u9fa5\w\@\&\-]/g,'')">
                                </div>
                                <div class="btn-group" role="group"><!--suppress ALL-->
                                    <input type="hidden" id="onlineStatus" th:value="${status}">
                                    <button id="onlineCss" type="button" class="btn dropdown-toggle"
                                            data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                        <span id="statusflag">全部</span>
                                        <span class="caret"></span>
                                    </button>
                                    <ul class="dropdown-menu">
                                        <li><a href="javascript:queryByStatus('')">全部</a></li>
                                        <li><a href="javascript:queryByStatus('1')">可用</a></li>
                                        <li><a href="javascript:queryByStatus('0')">禁用</a></li>
                                    </ul>
                                    <script th:inline="javascript">
                                        /* 设备状态下拉按钮预处理 */
                                        $(document).ready(function () {
                                            var status = [[${status}]];
                                            if (status == 1) {
                                                $("#statusflag").text("可用");
                                                $("#onlineCss").addClass("btn-info");
                                            } else if (status == 0) {
                                                $("#statusflag").text("禁用");
                                                $("#onlineCss").addClass("btn-danger");
                                            } else {
                                                $("#statusflag").text("全部");
                                                $("#onlineCss").addClass("btn-default");
                                            }
                                            return;
                                        });

                                        function queryByStatus(sta) {
                                            if (sta == '1') {
                                                $("#onlineStatus").val(1);
                                            } else if (sta == '0') {
                                                $("#onlineStatus").val(0);
                                            } else {
                                                $("#onlineStatus").val('');
                                            }
                                            querylist();
                                        }
                                    </script>
                                </div>
                                <div class="btn-group">
                                    <button id="queryBtn2" type="button" class="btn btn-primary"
                                            onclick="querylist()">
                                        查询
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                    <table class="table table-striped scroll-bar"
                           style="table-layout: fixed; display:block; height: 500px; overflow: auto">
                        <!--suppress ALL-->
                        <thead th:if="${dataPage.content.size() > 0}">
                        <tr>
                            <th><!--suppress ALL-->
                                <input type="checkbox" id="checkRow_head" name="checkRow_" value=""
                                       th:onclick="|checkPageSelected_(this);loadallids(this, ${dataPage.content})|"/>
                            </th>
                            <th>用户名</th>
                            <th>手机号</th>
                            <th>真实姓名</th>
                            <th>权限</th>
                            <th>状态</th>
                            <th>操作</th>
                        </tr>
                        </thead><!--suppress ALL-->
                        <tbody th:if="${dataPage.content.size() > 0}">
                        <!--suppress ALL-->
                        <tr th:each="user : ${dataPage.content}">
                            <th scope="row"><!--suppress ALL-->
                                <input type="checkbox" name="checkRow_"
                                       th:onclick="|checkRowSelected_(this);loadcheck(this, ${user.id})|"/>
                            </th><!--suppress ALL-->
                            <td th:text="${user.userName}" style="width: 15%;"></td><!--suppress ALL-->
                            <td th:text="${user.phone}" style="width: 15%;"></td><!--suppress ALL-->
                            <td th:text="${user.realName}" style="width: 15%;"></td><!--suppress ALL-->
                            <td th:text="${user.remark}" style="width: 20%;"></td><!--suppress ALL-->
                            <td style="width: 10%;"><!--suppress ALL-->
                                <label th:if="${user.status == 0}" style="color:red;">禁用</label><!--suppress ALL-->
                                <label th:if="${user.status == 1}" style="color:deepskyblue;">可用</label>
                            </td>
                            <td style="width: 25%;"><!--suppress ALL-->
                                <button type="button" class="btn-xs btn-primary"
                                        th:onclick="|userEdit(${user.id})|">
                                    权限设置
                                </button><!--suppress ALL-->
                                <button type="button" class="btn-xs btn-info" th:onclick="|resetPwd(${user.id})|">
                                    重置密码
                                </button><!--suppress ALL-->
                                <button th:if="${user.status == 0}" type="button" class="btn-xs btn-primary"
                                        th:onclick="|userEnable(${user.id})|">
                                    启用
                                </button><!--suppress ALL-->
                                <button th:if="${user.status == 1}" type="button" class="btn-xs btn-danger"
                                        th:onclick="|userUnable(${user.id})|">
                                    禁用
                                </button><!--suppress ALL-->
                                <button type="button" class="btn-xs btn-danger"
                                        th:onclick="|userDelete(${user.id})|">
                                    删除
                                </button>
                            </td>
                        </tr>
                        </tbody><!--suppress ALL-->
                        <tbody th:if="${dataPage.content.size() == 0}">
                        <div class="pagenodate" style="padding-top: 50px"><label style="color:red;">没有符合您要求的记录</label>
                        </div>
                        </tbody>
                    </table>
                    <!-- 分页组件 -->
                    <div th:replace="commons::common_paginater"></div>
                </div>
            </div>
        </form>
    </div>
</div>

<!-- 添加用户框 -->
<div class="modal fade" id="userDialog" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
     aria-hidden="true" data-backdrop="static">
    <div class="modal-dialog" style="width:550px;">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title" id="userModal">用户信息</h4>
            </div>
            <form class="form-horizontal" id="userinfo" name="userinfo" role="form" method="post"
                  enctype="multipart/form-data">
                <div class="modal-body text-nowrap">
                    <div class="form-group" style="margin-right: 0px;">
                        <div class="col-md-4" style="padding-right: 5%; text-align: right">
                            <label class="control-label" style="color:red;">*</label>&nbsp;&nbsp;
                            <label class="control-label" for="loginname">用户名：</label>
                        </div>
                        <div class="col-md-6" style="padding-left: 0px">
                            <input type="text" maxlength="50" class="form-control " id="loginname" name="loginname"
                                   placeholder="请填写用户名" value="">
                        </div>
                    </div>
                    <div class="form-group" style="margin-right: 0px;">
                        <div class="col-md-4" style="padding-right: 5%; text-align: right">
                            <label class="control-label" style="color:red;">*</label>&nbsp;&nbsp;
                            <label class="control-label" for="loginphone">手机号：</label>
                        </div>
                        <div class="col-md-6" style="padding-left: 0px">
                            <input type="tel" maxlength="11" class="form-control " id="loginphone" name="loginphone"
                                   placeholder="请填写手机号" value="">
                        </div>
                    </div>
                    <div class="form-group" style="margin-right: 0px;" id="loginPwdItem">
                        <div class="col-md-4" style="padding-right: 5%; text-align: right">
                            <label class="control-label" style="color:red;">*</label>&nbsp;&nbsp;
                            <label class="control-label" for="loginPwd">登录密码：</label>
                        </div>
                        <div class="col-sm-6" style="padding-left: 0px">
                            <input type="password" maxlength="50" class="form-control" id="loginPwd" name="loginPwd"
                                   placeholder="登录密码" value="">
                        </div>
                    </div>
                    <div class="form-group" style="margin-right: 0px;" id="loginConfirmItem">
                        <div class="col-md-4" style="padding-right: 5%; text-align: right">
                            <label class="control-label" style="color:red;">*</label>&nbsp;&nbsp;
                            <label class="control-label" for="loginConfirm">确认密码：</label>
                        </div>
                        <div class="col-sm-6" style="padding-left: 0px">
                            <input type="password" maxlength="50" class="form-control" id="loginConfirm"
                                   name="loginConfirm"
                                   placeholder="确认密码" value="">
                        </div>
                    </div>
                    <div class="form-group" style="margin-right: 0px;">
                        <div class="col-md-4" style="padding-right: 5%; text-align: right">
                            <label class="control-label" for="realname">真实姓名：</label>
                        </div>
                        <div class="col-md-6" style="padding-left: 0px">
                            <input type="text" maxlength="50" class="form-control " id="realname" name="realname"
                                   placeholder="请填写真实姓名" value="">
                        </div>
                    </div>
                    <div class="form-group" style="margin-right: 0px;">
                        <div class="col-md-4" style="padding-right: 5%; text-align: right">
                            <label class="control-label" for="userpermission">权限选择：</label>
                        </div>
                        <div class="btn-group col-md-6" style="padding-left: 0px;">
                            <select class="selectpicker" multiple data-none-selected-text="请选择站点" id="userpermission"
                                    name="userpermission">
                            </select>
                        </div>
                    </div>
                </div>
            </form>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" onclick="saveinfo()">保存</button>
                <button type="button" class="btn btn-default" onclick="closeinfo()">返回</button>
            </div>
        </div>
    </div>
</div>

<script th:src="@{/static/js/common.js}"></script>
<script th:src="@{/static/js/jquery-ui.min.js}"></script>
<script th:src="@{/static/js/jquery.ztree.all-3.5.min.js}"></script>
<script th:src="@{/static/js/bootstrap-table.js}"></script>
<script th:src="@{/static/js/bootstrap-table-zh-CN.js}"></script>
<script th:src="@{/static/js/bootstrap-select.min.js}"></script>
<script th:inline="javascript">
    /** 条件查询 */
    function querylist() {
        var username = $("#username").val();
        var phone = $("#phone").val();
        var onlineStatus = $("#onlineStatus").val();

        location.href = "/mlp/user/list?username=" + encodeURI(username) + "&phone=" + encodeURI(phone) + "&status=" + onlineStatus
            + "&page=1" + "&pageSize=" + $("#pageSize").val();
    }


    /** ----- 新增用户 start ----- **/
    /*打开新增用户弹窗*/
    function userEdit(id) {
        //获取站点列表
        $.ajax({
            type: "POST"
            , url: "/mlp/groupd/list"
            , data: {
                userId: id
            }
            , contentType: "application/x-www-form-urlencoded;charset=utf-8;"
            , dataType: "json"
            , cache: false
            , success: function (data) {
                if (data.resultCode == 'SUCCESS') {
                    var groupList = data.groupList;
                    var tableHtml = '';
                    $.each(groupList, function (index, group) {
                        tableHtml += '<option value="' + group.id + '" style="color:lightgrey;">' + group.name + '</option>';
                    })
                    $('#userpermission').html(tableHtml);

                    var user = data.user;
                    if (id != 0) {
                        theid = id;
                        //清除缓存
                        $('#loginname').val(user.userName);
                        $('#loginphone').val(user.phone);
                        $('#realname').val(user.realName);
                        $('#loginPwd').val(user.password);
                        $('#loginConfirm').val(user.password);
                        $('#userpermission').val(user.groupId.split(','));

                        $('#loginname').attr("readOnly", true);
                        $('#loginphone').attr("readOnly", true);
                        $('#realname').attr("readOnly", true);
                        $('#loginPwdItem').hide();
                        $('#loginConfirmItem').hide();
                    } else {
                        $('#loginname').attr("readOnly", false);
                        $('#loginphone').attr("readOnly", false);
                        $('#realname').attr("readOnly", false);
                        $('#loginPwdItem').show();
                        $('#loginConfirmItem').show();
                    }
                    //加载用户数据
                    $('.selectpicker').selectpicker('refresh');
                    $('.selectpicker').selectpicker('render');
                    $('#userDialog').modal('show');
                } else {
                    alert(data.resultMessage);
                }
            }
            , error: function (XMLHttpRequest, textStatus, errorThrown) {
                //alert(XMLHttpRequest.status + textStatus);
            }
        });

    }

    /*关闭新增用户弹窗*/
    function closeinfo() {
        $('#userDialog').modal('hide');

        //清除缓存
        theid = 0;
        $('#loginname').val("");
        $('#loginphone').val("");
        $('#realname').val("");
        $('#loginPwd').val("");
        $('#loginConfirm').val("");
        $('#userpermission').val("");
    }

    /*保存详情*/
    var theid = 0;

    function saveinfo() {
        var loginname = $('#loginname').val();
        var loginphone = $('#loginphone').val();
        var realname = $('#realname').val();
        var newPwd = $('#loginPwd').val();
        var newPwdConfirm = $('#loginConfirm').val();
        var userpermission = $('#userpermission').val();
        if (loginname == "") {
            alert("请填写用户名！");
            $('#loginname').focus();
            return;
        }
        if (loginphone == "") {
            alert("请填写手机号！");
            $('#loginphone').focus();
            return;
        }
        if (!isNum(loginphone)) {
            alert("手机号只能是数字！");
            $('#loginphone').focus();
            return;
        }
        if (newPwd == "") {
            alert("请填写登录密码！");
            $('#newPwd').focus();
            return;
        }
        if (newPwdConfirm == "") {
            alert("请填写确认密码！");
            $('#newPwdConfirm').focus();
            return;
        }
        if (newPwd != newPwdConfirm) {
            alert("确认密码与登录密码不一致！");
            $('#newPwdConfirm').focus();
            return;
        }
        var target = "";
        if (userpermission == null) {
            if (!confirm("您还没有设置用户权限，确定创建用户？")) {
                return;
            }
        } else {
            //遍历行数据index:行号(从0开始)；value:行数据对象
            $.each(userpermission, function (index, value) {
                if (index == 0) {
                    target = value;
                } else {
                    target += "," + value;
                }
            });
        }
        $.ajax({
            type: "POST"
            , url: "/mlp/user"
            , data: {
                id: theid
                , username: loginname
                , phone: loginphone
                , newPwd: newPwd
                , newPwdConfirm: newPwdConfirm
                , realname: realname
                , userpermission: target
            }
            , contentType: "application/x-www-form-urlencoded;charset=utf-8;"
            , dataType: "json"
            , cache: false
            , success: function (data) {
                if (data.resultCode == 'SUCCESS') {
                    location.reload();
                } else {
                    alert(data.resultMessage);
                }
            }
            , error: function (XMLHttpRequest, textStatus, errorThrown) {
                //alert(XMLHttpRequest.status + textStatus);
            }
        });
    }

    /** ----- 新增用户 end ----- **/


    /** ----- 用户操作 start ----- **/
    //重置密码
    function resetPwd(id) {
        editUserinfo(id, "1", "", "");
    }

    //启用用户
    function userEnable(id) {
        editUserinfo(id, "", "1", "");
    }

    //禁用用户
    function userUnable(id) {
        editUserinfo(id, "", "0", "");
    }

    //删除用户
    function userDelete(id) {
        editUserinfo(id, "", "", "1");
    }

    /*修改信息*/
    function editUserinfo(id, pwdReset, status, delflag) {
        var msg = "";
        if (pwdReset != "") {
            msg = "确定重置密码？";
        }
        if (status != "") {
            if (status == "0") {
                msg = "确定禁用该用户？";
            } else if (status == "1") {
                msg = "确定启用该用户？";
            }
        }
        if (delflag != "") {
            msg = "确定删除该用户？";
        }
        if (msg == "") {
            return;
        }

        if (confirm(msg)) {
            $.ajax({
                type: "PUT"
                , url: "/mlp/user"
                , data: {
                    id: id
                    , pwdReset: pwdReset
                    , status: status
                    , delflag: delflag
                }
                , contentType: "application/x-www-form-urlencoded;charset=utf-8;"
                , dataType: "json"
                , cache: false
                , success: function (data) {
                    if (pwdReset != "") {
                        alert("密码已重置为‘111111’");
                    }
                    location.reload();
                }
                , error: function (XMLHttpRequest, textStatus, errorThrown) {
                    //alert(XMLHttpRequest.status + textStatus);
                }
            });
        }
    }
    /** ----- 用户操作 end ----- **/

</script>
</body>
</html>
